﻿namespace Pyramid.Tests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Pyramid.Hardware;
    using Pyramid.Hardware.Interop;

    [TestClass]
    public class DecoderFixture
    {
        [TestMethod]
        public void ShouldDecodeTrueInputLine()
        {
            var inputSignal = Trivalent.True;
            var expectedOutput = "0t1ZZ";

            TestDecoder(inputSignal, expectedOutput);

        }

        [TestMethod]
        public void ShouldDecodeUnknownInputLine()
        {
            var inputSignal = Trivalent.Unknown;
            var expectedOutput = "0tZ1Z";

            TestDecoder(inputSignal, expectedOutput);
        }

        [TestMethod]
        public void ShouldDecodeFalseInputLine()
        {
            var inputSignal = Trivalent.False;
            var expectedOutput = "0tZZ1";

            TestDecoder(inputSignal, expectedOutput);
        }

        [TestMethod]
        public void ShouldDecodeDynamicInputLine()
        {
            VariableInputLine inputLine = new VariableInputLine();

            Decoder decoder = new Decoder(inputLine);

            var outputBus = new OutputBus(decoder.OutputBus);

            inputLine.SetInput(Trivalent.True);
            Assert.AreEqual(Tryte.Parse("0t1ZZ"), outputBus.Value.ToTryte());

            inputLine.SetInput(Trivalent.Unknown);

            Assert.AreEqual(Tryte.Parse("0tZ1Z"), outputBus.Value.ToTryte());

            inputLine.SetInput(Trivalent.False);
            Assert.AreEqual(Tryte.Parse("0tZZ1"), outputBus.Value.ToTryte());
        }

        private static void TestDecoder(Trivalent inputSignal, string expectedOutput)
        {
            var inputLine = new FixedInputLine(inputSignal);

            var decoder = new Decoder(inputLine);

            var outputBus = new OutputBus(decoder.OutputBus);

            Assert.AreEqual(Tryte.Parse(expectedOutput), outputBus.Value.ToTryte());
        }
    }
}
